package be.jas.mvc.base.controller;

import javax.validation.Valid;

import be.jas.mvc.base.PerspectiveService;
import be.jas.mvc.base.model.FileUploadModel;
import be.jas.mvc.base.model.fam.FamManager;
import be.jas.mvc.base.model.fam.FamPerspective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import be.jas.mvc.base.dto.FormDTO;

import java.io.File;
import java.io.IOException;
import java.util.List;

@SessionAttributes(value={"manager", "perspective"})
@Controller
public class HomeController {

    //Wiring
    @Autowired
    private PerspectiveService myPerspectiveService;

    //ModelAttributes
    @ModelAttribute
    public FormDTO createFormBean() {
        return new FormDTO();
    }


    @ModelAttribute
    public FileUploadModel createFileModel(){
        return new FileUploadModel();
    }



    //URL mappings
    //Home page
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String displayHome(@ModelAttribute FileUploadModel fileModel, ModelMap model) {
        model.put("fileModel", fileModel);
		return "home";
	}
	

	@RequestMapping(value = "/", method=RequestMethod.POST)
	public String submitMessage(@ModelAttribute FileUploadModel fileModel,
                                BindingResult result,
								SessionStatus sessionStatus,
								RedirectAttributes redirectAttrs,
                                ModelMap model) {
        if(fileModel.getFile() != null){
            File file = new File("temp");
            try {
                fileModel.getFile().transferTo(file);
                FamManager manager = myPerspectiveService.generateStoreFromFile(file);
                model.put("manager", manager);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }
		
		//return "redirect:/";
       return "perspectives";
        //return "debug";
	}
    //Perspectives page

    @RequestMapping(value = "/{perspective}", method = RequestMethod.GET)
    public String displayPerspectiveByName(@ModelAttribute("manager") FamManager manager, @PathVariable("perspective") String perspectiveName, ModelMap model) {
        if(model.get("perspective") != null){
            FamPerspective perspective = (FamPerspective)model.get("perspective");
            if(perspective.isValid()){
                model.put("perspective", manager.getPerspective(perspectiveName));
            }
        } else {
            model.put("perspective", manager.getPerspective(perspectiveName));
        }
        return "perspective";
    }

    @RequestMapping(value = "/saveperspective", method=RequestMethod.POST)
    public String submitPerspective(@ModelAttribute("perspective") FamPerspective perspective,
                                BindingResult result,
                                SessionStatus sessionStatus,
                                RedirectAttributes redirectAttrs,
                                ModelMap model) {
        //TODO Foutmeldingen toevoegen!


        //error from the solver
        List<String> errors = myPerspectiveService.ValidateRules((FamManager) model.get("manager"), perspective);

        for(String error : errors){
            result.reject(error, error);
            //result.rejectValue("featureMap[" + errorojbect.id + "].value");
        }


        if (!result.hasErrors()) {
            perspective.setValid(true);
            return "redirect:/" + perspective.getId();
        }
        /*String message = formDTO.toString();
        sessionStatus.setComplete();
        redirectAttrs.addFlashAttribute("message", message);*/
        perspective.setValid(false);
        return "perspective";
    }

    //Contact page
    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public ModelAndView displayContact(ModelMap model) {
        return new ModelAndView("contact");
    }

    //About page
    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public ModelAndView displayAbout(ModelMap model) {
        return new ModelAndView("about");
    }
}
